﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.Unity;
using Vit.Web.Services;

namespace Vit.Web.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class RangeAttribute : System.ComponentModel.DataAnnotations.RangeAttribute
    {
        public RangeAttribute(Type type, string min, string max)
            : base(type, min, max)
        {
        }

        public RangeAttribute(int min, int max)
            : base(min, max)
        {
        }

        public RangeAttribute(double min, double max)
            : base(min, max)
        {
        }

        public override string FormatErrorMessage(string name)
        {
            IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
            ILocalizationService localize = unity.Resolve<ILocalizationService>();
            if (localize != null)
            {
                name = localize.Resource(Area, name);
                string message = localize.Resource(Area, base.ErrorMessageString, new object[] { name, this.Minimum, this.Maximum });
                return string.Format(Thread.CurrentThread.CurrentCulture, message, new object[] { name, this.Minimum, this.Maximum });
            }
            else
                return base.FormatErrorMessage(name);
        }

        public string Area { get; set; }
    }
}
